/// 碳足迹记录数据模型
class CarbonFootprintRecord {
  final int? id;
  final String recordCode;
  final String recordName;
  final int processType;
  final double productionCapacity;
  final double energyConsumption;
  final double carbonEmission;
  final String? energySource;
  final double? equipmentEfficiency;
  final int? operationHours;
  final String? location;
  final String? projectPhase;
  final DateTime recordDate;
  final String? description;
  final int createdBy;
  final DateTime createdAt;
  final DateTime updatedAt;
  final bool isDeleted;

  CarbonFootprintRecord({
    this.id,
    required this.recordCode,
    required this.recordName,
    required this.processType,
    required this.productionCapacity,
    required this.energyConsumption,
    required this.carbonEmission,
    this.energySource,
    this.equipmentEfficiency,
    this.operationHours,
    this.location,
    this.projectPhase,
    required this.recordDate,
    this.description,
    required this.createdBy,
    required this.createdAt,
    required this.updatedAt,
    required this.isDeleted,
  });

  /// 从Map创建CarbonFootprintRecord对象
  factory CarbonFootprintRecord.fromMap(Map<String, dynamic> map) {
    return CarbonFootprintRecord(
      id: map['id'] as int?,
      recordCode: map['record_code'] as String,
      recordName: map['record_name'] as String,
      processType: map['process_type'] as int,
      productionCapacity: (map['production_capacity'] as num).toDouble(),
      energyConsumption: (map['energy_consumption'] as num).toDouble(),
      carbonEmission: (map['carbon_emission'] as num).toDouble(),
      energySource: map['energy_source'] as String?,
      equipmentEfficiency: map['equipment_efficiency'] != null 
          ? (map['equipment_efficiency'] as num).toDouble() 
          : null,
      operationHours: map['operation_hours'] as int?,
      location: map['location'] as String?,
      projectPhase: map['project_phase'] as String?,
      recordDate: DateTime.parse(map['record_date'] as String),
      description: map['description'] as String?,
      createdBy: map['created_by'] as int,
      createdAt: DateTime.parse(map['created_at'] as String),
      updatedAt: DateTime.parse(map['updated_at'] as String),
      isDeleted: (map['is_deleted'] as int) == 1,
    );
  }

  /// 转换为Map
  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'record_code': recordCode,
      'record_name': recordName,
      'process_type': processType,
      'production_capacity': productionCapacity,
      'energy_consumption': energyConsumption,
      'carbon_emission': carbonEmission,
      'energy_source': energySource,
      'equipment_efficiency': equipmentEfficiency,
      'operation_hours': operationHours,
      'location': location,
      'project_phase': projectPhase,
      'record_date': recordDate.toIso8601String().split('T')[0], // 只保留日期部分
      'description': description,
      'created_by': createdBy,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
      'is_deleted': isDeleted ? 1 : 0,
    };
  }

  /// 创建副本
  CarbonFootprintRecord copyWith({
    int? id,
    String? recordCode,
    String? recordName,
    int? processType,
    double? productionCapacity,
    double? energyConsumption,
    double? carbonEmission,
    String? energySource,
    double? equipmentEfficiency,
    int? operationHours,
    String? location,
    String? projectPhase,
    DateTime? recordDate,
    String? description,
    int? createdBy,
    DateTime? createdAt,
    DateTime? updatedAt,
    bool? isDeleted,
  }) {
    return CarbonFootprintRecord(
      id: id ?? this.id,
      recordCode: recordCode ?? this.recordCode,
      recordName: recordName ?? this.recordName,
      processType: processType ?? this.processType,
      productionCapacity: productionCapacity ?? this.productionCapacity,
      energyConsumption: energyConsumption ?? this.energyConsumption,
      carbonEmission: carbonEmission ?? this.carbonEmission,
      energySource: energySource ?? this.energySource,
      equipmentEfficiency: equipmentEfficiency ?? this.equipmentEfficiency,
      operationHours: operationHours ?? this.operationHours,
      location: location ?? this.location,
      projectPhase: projectPhase ?? this.projectPhase,
      recordDate: recordDate ?? this.recordDate,
      description: description ?? this.description,
      createdBy: createdBy ?? this.createdBy,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
      isDeleted: isDeleted ?? this.isDeleted,
    );
  }

  /// 获取工艺类型名称
  String get processTypeName {
    switch (processType) {
      case 1:
        return '电解水制氢';
      case 2:
        return '天然气制氢';
      case 3:
        return '煤制氢';
      case 4:
        return '生物质制氢';
      default:
        return '未知工艺';
    }
  }

  /// 获取排放强度 (吨CO2/吨)
  double get emissionIntensity {
    if (productionCapacity <= 0) return 0.0;
    return carbonEmission / productionCapacity;
  }

  /// 获取能源强度 (kWh/吨)
  double get energyIntensity {
    if (productionCapacity <= 0) return 0.0;
    return energyConsumption / productionCapacity;
  }

  /// 验证记录名称
  static String? validateRecordName(String? value) {
    if (value == null || value.isEmpty) {
      return '记录名称不能为空';
    }
    if (value.length < 2) {
      return '记录名称至少2个字符';
    }
    if (value.length > 200) {
      return '记录名称不能超过200个字符';
    }
    return null;
  }

  /// 验证产能
  static String? validateProductionCapacity(String? value) {
    if (value == null || value.isEmpty) {
      return '产能不能为空';
    }
    final capacity = double.tryParse(value);
    if (capacity == null) {
      return '产能必须是数字';
    }
    if (capacity < 0.01) {
      return '产能不能小于0.01';
    }
    if (capacity > 1000000.0) {
      return '产能不能超过1,000,000';
    }
    return null;
  }

  /// 验证能耗
  static String? validateEnergyConsumption(String? value) {
    if (value == null || value.isEmpty) {
      return '能耗不能为空';
    }
    final consumption = double.tryParse(value);
    if (consumption == null) {
      return '能耗必须是数字';
    }
    if (consumption < 0.0) {
      return '能耗不能小于0';
    }
    if (consumption > 1000000.0) {
      return '能耗不能超过1,000,000';
    }
    return null;
  }

  /// 验证碳排放量
  static String? validateCarbonEmission(String? value) {
    if (value == null || value.isEmpty) {
      return '碳排放量不能为空';
    }
    final emission = double.tryParse(value);
    if (emission == null) {
      return '碳排放量必须是数字';
    }
    if (emission < 0.0) {
      return '碳排放量不能小于0';
    }
    if (emission > 1000.0) {
      return '碳排放量不能超过1,000';
    }
    return null;
  }

  /// 验证设备效率
  static String? validateEquipmentEfficiency(String? value) {
    if (value == null || value.isEmpty) {
      return null; // 设备效率可选
    }
    final efficiency = double.tryParse(value);
    if (efficiency == null) {
      return '设备效率必须是数字';
    }
    if (efficiency < 0.0 || efficiency > 100.0) {
      return '设备效率必须在0-100之间';
    }
    return null;
  }

  /// 验证运行时间
  static String? validateOperationHours(String? value) {
    if (value == null || value.isEmpty) {
      return null; // 运行时间可选
    }
    final hours = int.tryParse(value);
    if (hours == null) {
      return '运行时间必须是整数';
    }
    if (hours < 0 || hours > 8760) {
      return '运行时间必须在0-8760之间';
    }
    return null;
  }

  /// 验证地理位置
  static String? validateLocation(String? value) {
    if (value == null || value.isEmpty) {
      return null; // 地理位置可选
    }
    if (value.length > 200) {
      return '地理位置不能超过200个字符';
    }
    return null;
  }

  /// 验证项目阶段
  static String? validateProjectPhase(String? value) {
    if (value == null || value.isEmpty) {
      return null; // 项目阶段可选
    }
    if (value.length > 50) {
      return '项目阶段不能超过50个字符';
    }
    return null;
  }

  /// 验证工艺类型
  static String? validateProcessType(int? value) {
    if (value == null) {
      return '工艺类型不能为空';
    }
    if (value < 1 || value > 4) {
      return '工艺类型选择无效';
    }
    return null;
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is CarbonFootprintRecord &&
        other.id == id &&
        other.recordCode == recordCode;
  }

  @override
  int get hashCode {
    return id.hashCode ^ recordCode.hashCode;
  }

  @override
  String toString() {
    return 'CarbonFootprintRecord(id: $id, recordCode: $recordCode, recordName: $recordName, processType: $processTypeName)';
  }
} 